package com.wwwarehouse.carddesk.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wwwarehouse.carddesk.R;
import com.wwwarehouse.common.bean.response.CardDeskTaskResponseBean;

import java.util.List;

public class TaskDetailsAdapter extends BaseAdapter {
    private Context mContext;
    private List<CardDeskTaskResponseBean.TaskBean> mCardDeskTaskList;
    private int mItemHeight;

    public TaskDetailsAdapter(Context context, List<CardDeskTaskResponseBean.TaskBean> cardDeskTaskList, int itemHeight) {
        this.mContext = context;
        this.mCardDeskTaskList = cardDeskTaskList;
        this.mItemHeight = itemHeight;
    }

    @Override
    public int getCount() {
        return mCardDeskTaskList == null ? 0 : mCardDeskTaskList.size();
    }

    @Override
    public Object getItem(int position) {
        return mCardDeskTaskList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        CardDeskTaskResponseBean.TaskBean taskBean = mCardDeskTaskList.get(position);
        ViewHolder holder;

        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_task_details, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);

            AbsListView.LayoutParams params = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            params.height = mItemHeight;
            holder.rootLayout.setLayoutParams(params);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.labelTxt.setText(taskBean.getCardName());
        holder.descriptionTxt.setText(taskBean.getCardExplain());
        holder.timeoutTxt.setText(taskBean.getTimeout());

        if (!TextUtils.isEmpty(taskBean.getTaskHandedMan())) {
            holder.nameTxt.setVisibility(View.VISIBLE);
            holder.nameTxt.setText(taskBean.getTaskHandedMan());
        } else {
            holder.nameTxt.setVisibility(View.INVISIBLE);
        }

        if ("1".equals(taskBean.getEmergencyDegree())) {
            convertView.setBackgroundResource(R.drawable.selector_task_timeout_bg);

            holder.labelTxt.setTextColor(ContextCompat.getColor(mContext,R.color.common_color_c3_ffffff));
            holder.descriptionTxt.setTextColor(ContextCompat.getColor(mContext,R.color.common_color_c3_ffffff));
            holder.timeoutTxt.setTextColor(ContextCompat.getColor(mContext,R.color.common_color_c3_ffffff));
            holder.nameTxt.setTextColor(ContextCompat.getColor(mContext,R.color.common_color_c3_ffffff));
            holder.unitTxt.setTextColor(ContextCompat.getColor(mContext,R.color.common_color_c3_ffffff));
        } else if ("2".equals(taskBean.getEmergencyDegree())) {
            convertView.setBackgroundResource(R.drawable.selector_task_urgent_bg);

            holder.labelTxt.setTextColor(ContextCompat.getColor(mContext,R.color.common_color_c3_ffffff));
            holder.descriptionTxt.setTextColor(ContextCompat.getColor(mContext,R.color.common_color_c3_ffffff));
            holder.timeoutTxt.setTextColor(ContextCompat.getColor(mContext,R.color.common_color_c3_ffffff));
            holder.nameTxt.setTextColor(ContextCompat.getColor(mContext,R.color.common_color_c3_ffffff));
            holder.unitTxt.setTextColor(ContextCompat.getColor(mContext,R.color.common_color_c3_ffffff));
        }

        if ("1".equals(taskBean.getTaskBack())) {
            holder.returnImg.setVisibility(View.VISIBLE);
        } else {
            holder.returnImg.setVisibility(View.GONE);
        }

        if ("Y".equals(taskBean.getBusinessIsShow())) {
            holder.unitTxt.setVisibility(View.VISIBLE);
            holder.unitTxt.setText(taskBean.getBelongBusinessName());
        } else {
            holder.unitTxt.setVisibility(View.INVISIBLE);
        }

        return convertView;
    }

    private class ViewHolder {
        private RelativeLayout rootLayout;

        private TextView labelTxt;
        private TextView descriptionTxt;
        private TextView timeoutTxt;
        private TextView nameTxt;
        private TextView unitTxt;

        private ImageView returnImg;
        public ViewHolder(View view){
            rootLayout = (RelativeLayout) view.findViewById(R.id.rl_root);
            labelTxt = (TextView) view.findViewById(R.id.tv_label);
            descriptionTxt = (TextView) view.findViewById(R.id.tv_description);
            timeoutTxt = (TextView) view.findViewById(R.id.tv_timeout);
            nameTxt = (TextView) view.findViewById(R.id.tv_name);
            unitTxt = (TextView) view.findViewById(R.id.tv_unit);

            returnImg = (ImageView) view.findViewById(R.id.iv_return);
        }
    }
}